import {Component} from 'angular2/core';
import {UserService} from '../services/user.service';
import {User} from '../models/user';
import {TaskInfo} from '../models/taskInfo';
import {AddTaskInfoService} from '../services/add-task.service';
import {TaskService} from '../services/task.service';
import {RouteParams} from 'angular2/router';

@Component({
  selector: 'update-task',
  providers: [AddTaskInfoService],
  templateUrl: 'app/components/update-task.component.html'
})
export class UpdateTaskInfo {
  taskInfo: TaskInfo = new TaskInfo();
  taskInfoAdded: boolean = false;
  user: User;

  constructor (
    private addTaskInfoService: AddTaskInfoService,
    private userService: UserService,
    private routeParams: RouteParams,
    private taskService:TaskService
  ) {
     let taskId = Number.parseInt(this.routeParams.get('id'));
     this.taskService.getTaskInfoById(taskId).subscribe(
      task => {
        this.taskInfo = JSON.parse(JSON.parse(JSON.stringify(task))._body);
        console.log(this.taskInfo);
      },
      error => console.log(error)
    )
    }

  onSubmit() {
    this.userService.getUserByName(localStorage.getItem("currentUserName")).subscribe(
      user => {
        this.user = JSON.parse(JSON.parse(JSON.stringify(user))._body);
        console.log(this.user);
        this.taskInfo.userId = this.user.userId;
        this.taskService.updateTask(this.taskInfo)
        .subscribe(
          data => {
            this.taskInfoAdded = true;
            //this.taskInfo = new TaskInfo();
          },
          error => console.log(error)
        );
      },
      error => console.log(error)
    )
  }
}
